﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BaseCollectionModel.cs" company="XamlNinja">
//   2011 Richard Griffin and Ollie Riches
// </copyright>
// <summary>
//   Defines the BaseCollectionModel type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WP7Contrib.Common
{
    using System.Collections.Specialized;
    using System.Runtime.Serialization;

    /// <summary>
    /// Base collection model providing methods to raise property and collection change events.
    /// </summary>
    [DataContract]
    public abstract class BaseCollectionModel : BaseModel, INotifyCollectionChanged
    {
        /// <summary>
        /// The collection changed.
        /// </summary>
        public event NotifyCollectionChangedEventHandler CollectionChanged;

        /// <summary>
        /// The raise collection changed.
        /// </summary>
        /// <param name="action">
        /// The action.
        /// </param>
        protected void RaiseCollectionChanged(NotifyCollectionChangedAction action)
        {
            var collectionChanged = this.CollectionChanged;
            if (collectionChanged == null)
            {
                return;
            }

            collectionChanged(this, new NotifyCollectionChangedEventArgs(action));
        }

        /// <summary>
        /// The raise collection changed.
        /// </summary>
        /// <param name="action">
        /// The action.
        /// </param>
        /// <param name="changedItem">
        /// The changed item.
        /// </param>
        /// <param name="index">
        /// The index.
        /// </param>
        protected void RaiseCollectionChanged(NotifyCollectionChangedAction action, object changedItem, int index)
        {
            var collectionChanged = this.CollectionChanged;
            if (collectionChanged == null)
            {
                return;
            }

            collectionChanged(this, new NotifyCollectionChangedEventArgs(action, changedItem, index));
        }

        /// <summary>
        /// The raise collection changed.
        /// </summary>
        /// <param name="action">
        /// The action.
        /// </param>
        /// <param name="newItem">
        /// The new item.
        /// </param>
        /// <param name="changedItem">
        /// The changed item.
        /// </param>
        /// <param name="index">
        /// The index.
        /// </param>
        protected void RaiseCollectionChanged(NotifyCollectionChangedAction action, object newItem, object changedItem, int index)
        {
            var collectionChanged = this.CollectionChanged;
            if (collectionChanged == null)
            {
                return;
            }

            collectionChanged(this, new NotifyCollectionChangedEventArgs(action, newItem, changedItem, index));
        }
    }
}